import { _decorator, Component, find, instantiate, Node, Prefab, Sprite } from 'cc';
import { ResMgr } from '../Managers/ResMgr';
const { ccclass, property } = _decorator;

export class Game extends Component {
    private static _instance: Game;
    private canvas:Node|null = null;
    private progressBar: Sprite|null = null;
    static get instance() {
        if (this._instance == null) {
            this._instance = new Game();
        }
        return this._instance;
    }

    onLoad(): void {
        this.canvas = find("Canvas"); 
        this.progressBar = this.canvas?.getChildByName("ResLoading/UIProgress/value").getComponent(Sprite)
        this.progressBar.fillRange = 0;
    }

    //游戏开始的入口
    public GameStart(): void{
        //预加载资源
        //进度条
        var resPkg = {
            Maps:{
                assetType: Prefab,
                urls: [
                    "Prefabs/Fisher001"
                ]
            },
            GUI:{
                assetType: Prefab,
                urls: [
                    "UIPrefabs/GameUI"
                ]
            },
        }
        ResMgr.instance.preloadResPkg(resPkg, (now: number, total: number)=>{
            var per = now/total;
            if(this.progressBar !== null){
                this.progressBar.fillRange = per;
            }
        }, ()=>{
            this.canvas?.destroyAllChildren();
            this.EnterLoginScene()
        });
        
    }
    
    public EnterLoginScene():void{
        //释放3D地图
        // var mapPrefab = ResMgr.instance.getAsset("Maps", "Prefabs/Fisher001");
        // var map = instantiate(mapPrefab);
        // this.node.addChild(map);
        //释放UI视图
        var uiPrefab = ResMgr.instance.getAsset("GUI", "UIPrefabs/GameUI");
        var uiView = instantiate(uiPrefab);
        this.canvas?.addChild(uiView);
    }
}


